<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;
use DB;
use Illuminate\Support\Facades\Redis;

class Huati extends Model
{
    protected $table = 'tbl_huati';
    public $timestamps = false;

    /**
     * 编辑话题
     * @param $huati_id
     * @param $title
     * @param $title2
     * @param $introduce
     * @param $pic
     * @return array
     */
    static function HuatiModify($huati_id, $introduce, $pic)
    {
        $huati = Huati::find($huati_id);
        if (!$huati) {
            return error('没有这个圈子');
        }

        $huati->introduce = $introduce;
        if ($pic) {
            $huati->pic = Storage::url($pic->store('/', 'oss'));
        }
        if (!$huati->save()) {
            return error('修改失败');
        }

        return success(['message' => '修改成功']);
    }


    /**
     * 话题详情
     * @param $huati_id
     * @return array
     */
    static function HuatiInfo($huati_id)
    {
        $huati = Huati::select('title', 'title2', 'introduce', 'pic')->findOrFail($huati_id);
        if (!$huati) {
            return error('没有这个圈子');
        }
        $look_num = Redis::get('HT_LL_' . $huati_id);
        $info = [
            'title' => $huati->title,
            'title2' => $huati->title2,
            'introduce' => $huati->introduce,
            'pic' => $huati->pic,
            'sum_num' => Postings::where('huati_id', $huati_id)->where('is_view', Postings::VIEW_YES)->count(),
            'look_num' => $look_num ? $look_num : 0,
            'follow_num' => DB::table('tbl_huati_follow')->where('huati_id', $huati_id)->count(),
        ];

        return success(compact('info'));
    }
}
